package com.developteam.xnmall.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * Description:
 * Author: ZhangXirong 27216
 * Version: 1.0
 * Create Date Time: 2022/3/16 23:21.
 * Update Date Time:
 *
 * @see
 */

@Component
public class SetTimeInterceptor implements HandlerInterceptor {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");  //非受限资源一续命，二全部放行
        if(token != null){
            String s = stringRedisTemplate.boundValueOps(token).get();
            if (s != null) {
                //给token续命
                stringRedisTemplate.boundValueOps(token).expire(1, TimeUnit.MINUTES);
                return true;
            }
        }
        return true; //全部放行
    }
}
